unit unitnuevocobro;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  sqldb, db, unitformbusquedaempresa, unitmainform, LR_Class, Dateutils;

type

  { TFormNuevoCobro }

  TFormNuevoCobro = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    frReport1: TfrReport;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    SQLQuery1: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure frReport1GetValue(const ParName: String; var ParValue: Variant);
  private
    codigo_empresa : string;
    codigo_cobro : string;
    fecha : string;
    hora : string;
    retira : string;
  public

  end;

var
  FormNuevoCobro: TFormNuevoCobro;

implementation

{$R *.lfm}

{ TFormNuevoCobro }

procedure TFormNuevoCobro.Button1Click(Sender: TObject);
begin
  FormBusquedaEmpresa.busqueda_nombre := '';
  FormBusquedaEmpresa.busqueda_nombre := InputBox('Busqueda de Empresa', 'Ingrese parte del nombre:', '');
  if FormBusquedaEmpresa.busqueda_nombre <> '' then
  begin
    FormBusquedaEmpresa.ShowModal;
    Edit1.Text := FormbusquedaEmpresa.resultado_nombre;
    codigo_empresa := FormbusquedaEmpresa.resultado_codigo;
    Edit2.SetFocus;
  end
  else
    FormBusquedaEmpresa.busqueda_nombre := '';
end;

procedure TFormNuevoCobro.Button2Click(Sender: TObject);
var
  nueva_empresa : string;
  QueryString : string;

begin
  nueva_empresa := InputBox('Alta de Empresa', 'Ingrese nombre:', '');

  if nueva_empresa <> '' then
  begin
    nueva_empresa := uppercase(nueva_empresa);
    QueryString := 'insert into empresa(emp_nombre) values(''' + nueva_empresa + ''')';
    MainForm.PQConnection1.ExecuteDirect('BEGIN WORK');
    MainForm.PQConnection1.ExecuteDirect(QueryString);
    MainForm.PQConnection1.ExecuteDirect('COMMIT WORK');

    Edit1.Text := nueva_empresa;
    Edit2.SetFocus;

    if SQLQuery1.Active = true then
     SQLQuery1.Close;

    SQLQuery1.SQL.Text := 'select emp_cdg from empresa where emp_nombre = ''' + nueva_empresa + '''';

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      codigo_empresa := SQLQuery1.Fields[0].AsString;
      SQLQuery1.Next
    end;

    SQLQuery1.Close;
  end;
end;

procedure TFormNuevoCobro.Button3Click(Sender: TObject);
var
  QueryString : string;
begin
  if (Edit1.Text <> '') and (Edit2.Text <> '') and (Edit3.Text <> '') and (Edit5.Text <> '') then
  begin
    Edit3.Text := uppercase(Edit3.text);
    QueryString := 'insert into cobro(crb_empresa, crb_monto, crb_persona, crb_nrocuenta, crb_telcontacto) values('
      + codigo_empresa + ', ' + Edit2.Text + ', ''' + Edit3.Text + ''', ''' + Edit4.text + ''', ''' + Edit5.Text
      +  ''')';
    MainForm.PQConnection1.ExecuteDirect('BEGIN WORK');
    MainForm.PQConnection1.ExecuteDirect(QueryString);
    MainForm.PQConnection1.ExecuteDirect('COMMIT WORK');

    if SQLQuery1.Active = true then
      SQLQuery1.Close;

    SQLQuery1.SQL.Text := 'select max(crb_cdg) from cobro';

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      codigo_cobro := SQLQuery1.Fields[0].AsString;
      SQLQuery1.Next;
    end;

    SQLQuery1.Close;

    SQLQuery1.SQL.Text := 'select crb_fecha, crb_hora from vw_cobro where crb_cdg = ' + codigo_cobro;

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      fecha := SQLQuery1.Fields[0].AsString;
      hora := SQLQuery1.Fields[1].AsString;
      SQLQuery1.Next;
    end;

    if time < StrToTime('12:30:00') then
      retira := DateToStr(Date) + ' 17:30hs'
    else
      retira := DateToStr(IncDay(Date, +1)) + ' 11:30hs';

    frReport1.LoadFromFile('ticket.lrf');
    frReport1.PrepareReport;
    frReport1.PrintPreparedReport('', 1);

    ShowMessage('GENERAR TALON PARA EL COBRADOR');

    frReport1.LoadFromFile('talon.lrf');
    frReport1.PrepareReport;
    frReport1.PrintPreparedReport('', 1);

    Close();
  end
  else
    ShowMessage('ERROR EN LA CARGA DE DATOS');
end;

procedure TFormNuevoCobro.Button4Click(Sender: TObject);
begin
  Close;
end;

procedure TFormNuevoCobro.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;
end;

procedure TFormNuevoCobro.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := MainForm.PQConnection1;
  SQLQuery1.Transaction := MainForm.SQLTransaction1;
end;

procedure TFormNuevoCobro.FormShow(Sender: TObject);
begin
  codigo_cobro := '';
  codigo_empresa := '';
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Edit4.Text := '';
  Edit5.Text := '';
  Button1.SetFocus;
  fecha := '';
  hora := '';
  retira := '';
end;

procedure TFormNuevoCobro.frReport1GetValue(const ParName: String;
  var ParValue: Variant);
begin
  if ParName = 'codigo' then
    ParValue := codigo_cobro;

  if ParName = 'fecha' then
    ParValue := fecha;

  if ParName = 'hora' then
    ParValue := hora;

  if ParName = 'empresa' then
    parValue := Edit1.Text;

  if ParName = 'monto' then
    ParValue := Edit2.Text;

  if ParName = 'persona' then
    ParValue := Edit3.Text;

  if ParName = 'retira' then
    ParValue := retira;

  if ParName = 'nrocuenta' then
    ParValue := Edit4.Text;

  if ParName = 'telcontacto' then
    ParValue := Edit5.Text
end;

end.

